﻿// Copyright (C) Jari Hautio <jari.hautio@iki.fi> 2008. Licensed under GPLv2. See LICENSE.txt in solution folder.
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace ColorLib
{
    /// <summary>
    /// Fast color to color conversion table, which uses flat 6 bit per color conversion table.
    /// </summary>
    public class FastColorTable
    {
        List<Color> colors;
        int shiftPerSlot = 2;
        int bitsPerColor = 6;
        public int BitsPerColor { get { return bitsPerColor; } }
        public FastColorTable(int colorBits)
        {
            // limit to six bit colors to keep memory requirements realistic
            if (colorBits > 6) colorBits = 6;
            if (colorBits < 1) colorBits = 1;
            bitsPerColor = colorBits;
            shiftPerSlot = 8 - colorBits;
            int slotSize = 1 << shiftPerSlot;
            int size = 256 * 256 * 256 / (slotSize * slotSize * slotSize);
            colors = new List<Color>(size);
            for (int i = 0; i < size; i++)
                colors.Add( Color.Empty );
        }
        int GetPos(Color c)
        {
            return ((c.R >> shiftPerSlot) << (2 * bitsPerColor)) +
                ((c.G >> shiftPerSlot) << bitsPerColor) + 
                (c.B >> shiftPerSlot);
        }
        public void Add(Color c1, Color c2)
        {
            colors[GetPos(c1)] = c2;
        }
        public Color Get(Color c)
        {
            return colors[GetPos(c)];
        }
    }
}
